﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.DNPC.Contract;
using DataAccess.DNPC.Core;
using System.Data.Entity;
using DataAccess.UnitTransaction.Contract;
using Business.ValueObject.Context;
using DataAccess.Model;
using DataAccess.Contract;
using DataAccess.Core;
using System.Data.Entity.Validation;

namespace DataAccess.UnitTransaction.Core
{
    public class UnitTransaction : IUnitTransaction
    {
        private DNPCEntities _dnpcContext;
        private IDAOStatusDetailVehicles _daoStatusDetailVehicles;
        private IDAOPerformanceReportVehicles _daoPerformanceReportVehicles;
        private IDAOReportVehicles _daoReportVehicles;
        private bool _disposed = false;
        private VOServiceContext _repositoryContext;

        public UnitTransaction()
        {
            _dnpcContext = new DNPCEntities();
        }

        public void SetContext(VOServiceContext repositoryContext)
        {
            _repositoryContext = repositoryContext;
        }

        public IDAOStatusDetailVehicles DAOStatusDetailVehicles
        {
            get
            {
                if (_daoStatusDetailVehicles == null)
                {
                    _daoStatusDetailVehicles = new DAOStatusDetailVehicles(_dnpcContext, _repositoryContext);
                }

                return _daoStatusDetailVehicles;
            }
        }

        public IDAOPerformanceReportVehicles DAOPerformanceReportVehicles
        {
            get
            {
                if (_daoPerformanceReportVehicles == null)
                {
                    _daoPerformanceReportVehicles = new DAOPerformanceReportVehicles(_dnpcContext, _repositoryContext);
                }

                return _daoPerformanceReportVehicles;
            }
        }

        public IDAOReportVehicles DAOReportVehicles
        {
            get
            {
                if (_daoReportVehicles == null)
                {
                    _daoReportVehicles = new DAOReportVehicles(_dnpcContext, _repositoryContext);
                }

                return _daoReportVehicles;
            }
        }

        public void Save()
        {
            _dnpcContext.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _dnpcContext.Dispose();
                }
            }

            _disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }
}
